# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : role_view.py
# Time       ：2023/7/9 16:38
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    角色视图
    role views
"""
from flask import request
from flask_restx import fields

from apis.system_oauth import system_oauth_ns
from apis.system_oauth.models import SystemRole, SystemRolePermissionRelation
from apis.system_oauth.schema.role_schema import SystemRoleSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

role = {
    'role_name': fields.String(description='角色名称'),
    'desc': fields.String(description='角色描述'),
}

role_marshal = MarshalModel('role', system_oauth_ns)


class RoleDetailView(BaseDetailsView):
    url = '/role/detail'

    def __init__(self, *args, **kwargs):
        super(RoleDetailView, self).__init__(*args, **kwargs)
        self.table_orm = SystemRole
        self.table_schema = SystemRoleSchema

    @handle_api_error()
    @system_oauth_ns.doc(description="获取角色详情")
    @system_oauth_ns.expect(role_marshal.detail_request())
    @system_oauth_ns.marshal_with(role_marshal.detail_response(role))
    def get(self):
        """获取角色详情"""
        return super(RoleDetailView, self).get()


class RoleView(BaseView):
    url = '/role'

    def __init__(self, *args, **kwargs):
        super(RoleView, self).__init__(*args, **kwargs)
        self.table_orm = SystemRole
        self.table_schema = SystemRoleSchema

    def handle_response_data(self):
        permission_ids = self.form_data.get('permission_ids')
        if request.method in ['POST', 'PUT'] and permission_ids:
            for permission_id in permission_ids:
                obj = SystemRolePermissionRelation(permission_id=permission_id, role_id=self.table_id)
                db.session.add(obj)
                db.session.commit()

        elif request.method == 'DELETE':
            with db.auto_commit():
                db.session.query(SystemRolePermissionRelation).filter(
                    SystemRolePermissionRelation.role_id == self.table_id).update({'active': 0})

    @handle_api_error()
    @system_oauth_ns.doc(description='获取角色列表')
    @system_oauth_ns.expect(role_marshal.list_request(role))
    @system_oauth_ns.marshal_with(role_marshal.list_response(role))
    def get(self):
        """获取角色列表"""
        return super(RoleView, self).get()

    @handle_api_error()
    @system_oauth_ns.doc(description='创建角色')
    @system_oauth_ns.expect(role_marshal.create_request(role))
    @system_oauth_ns.marshal_with(role_marshal.create_response())
    def post(self):
        """创建角色"""
        return super(RoleView, self).post()

    @handle_api_error()
    @system_oauth_ns.doc(description='修改角色')
    @system_oauth_ns.expect(role_marshal.update_request(role))
    @system_oauth_ns.marshal_with(role_marshal.update_response())
    def put(self):
        """修改角色"""
        return super(RoleView, self).put()

    @handle_api_error()
    @system_oauth_ns.doc(description='删除角色')
    @system_oauth_ns.expect(role_marshal.delete_request())
    @system_oauth_ns.marshal_with(role_marshal.delete_response())
    def delete(self):
        """删除角色"""
        return super(RoleView, self).delete()
